#ifndef __LINKTREE_H_
#define __LINKTREE_H_

#include "DoubleLinkList.h"
#include <string>
class LinkTree;
class TreeNode //树的结点
{
public:
    using ElementType = void*;
    TreeNode(const ElementType& element);
    
    //将一个结点绑定成另一个结点的孩子
    void Connect(TreeNode *parent);
    void Disconnect(TreeNode* parent);
    TreeNode* GetParent(){return parent;}
    ElementType GetData(){return data;}
    void SetData(const ElementType& element){data = element;}
    friend LinkTree;
private:
    ElementType data;//数据域
    TreeNode *parent;//双亲结点
    TreeNode *FirstChild;//第一个孩子
    TreeNode *nextSbling;//下一个兄弟指针
};

class LinkTree
{
public:
    using ElementType = TreeNode::ElementType;

    void SetPrint( void (*Print)(void *))
    {
        this->Print = Print;
    }
    void SetCompare (int (*Compare)(void * ,void *))
    {
        this->Compare = Compare;
    }
    void SetFree(void (*Free)(void *))
    {
        this->Free = Free;
    }
    void SetCopy(ElementType (*Copy)(ElementType))
    {
        this->Copy = Copy;
    }

    LinkTree();
    ~LinkTree();
    TreeNode *Getroot(){return root;}
    void show();
    
    DoubleLinkList FindByElement(const ElementType & element);
    int GetTreeHight();
    //统计树中结点数量
    int Size();
    //统计树中叶子结点数量
    int LeafSize();

private:
    void GetLeafNode(TreeNode* node,int &conut);
    void GetNodeCount(TreeNode* node,int &conut);
    int GetTreeHight(TreeNode* node);
    void FindNode(TreeNode* node,const ElementType& element,DoubleLinkList &list);
    void show(TreeNode *node,int deepth);
    void FreeNode(TreeNode* node);
    TreeNode *root;//根结点
    void (*Print)(void *);
    int (*Compare)(ElementType element1 ,ElementType element2);
    void (*Free)(void *);
    ElementType (*Copy)(ElementType);

};

#endif